{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" href="SHOP_CSS/register.css">
{/block}
{block name="body"}
<div class="layui-layout layui-layout-admin">
    <div class="apply-header">
        <div class="apply-header-box">
            <div class="apply-header-title">
                <a href="{:url('shop/index/index')}">
                    {notempty name="$website_info.logo"}
                    <img src="{:img($website_info.logo)}">
                    {else/}
                    <img src="HOME_IMG/shop_logo.png">
                    {/notempty}
                </a>
            </div>
        </div>
    </div>
</div>
<div class="register-body">
    <div class="layui-form register-content">
        <h2>店铺注册</h2>
        <div class="register-input register-info">
            <span>账号：</span>
            <input type="text" name="username" lay-verify="userName" placeholder="请输入账号" autocomplete="off" class="layui-input">
        </div>
        <div class="register-input register-info">
            <span>密码：</span>
            <input type="password" id="set_pass" name="password" lay-verify="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
        <div class="register-input register-info">
            <span>确认密码：</span>
            <input type="password" lay-verify="password|repass" placeholder="请确认密码" autocomplete="off" class="layui-input">
        </div>
		
		{if $shop_login == 1}
        <div class="register-input register-verification">
            <input type="text" name="captcha" lay-verify="verificationCode" placeholder="请输入验证码" autocomplete="off" class="layui-input">
            <div class="register-verify-code-img">
                <img id='verify_img' src="{$captcha['img']}" alt='captcha' onclick="verificationCode()"/>
            </div>
        </div>
		{/if}
		
        <button type="button" id="reg_btn" class="layui-btn ns-bg-color" lay-submit lay-filter="register_account">点击申请</button>
        <p class="operation-register">已有账号？<a href="javascript:;" class="ns-text-color" onclick="login()">&nbsp;立即登录</a></p>
    </div>

    <div class="ns-register-bottom">
        <a class="ns-footer-img" href="#"><img src="{if !empty($copyright.logo)} {:img($copyright.logo)} {else /}__STATIC__/img/copyright_logo.png{/if}" /></a>
        <p>{notempty name="$copyright.company_name"}{$copyright.company_name}{else/}上海牛之云网络科技有限公司{/notempty} {notempty name="$copyright.icp"}<a href={$copyright.copyright_link}>&nbsp;&nbsp;备案号{$copyright.icp}</a>{/notempty}</p>
        {notempty name="$copyright.gov_record"}<a class="gov-box" href={$copyright.gov_url}><img src="HOME_IMG/gov_record.png" alt="">公安备案{$copyright.gov_record}</a>{/notempty}
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
var form, register_repeat_flag = false;

	/**
	 * 验证码
	 */
	function verificationCode(){
		$.ajax({
			type: "get",
			url: "{:url('shop/login/captcha')}",
			dataType: "JSON",
			async: false,
			success: function (res) {
				var data = res.data;
				$("#verify_img").attr("src",data.img);
				$("input[name='captcha_id']").val(data.id);
			}
		});
	}

    layui.use('form', function(){
        form = layui.form;
		form.render();
		
        /* 注册*/
        form.on('submit(register_account)', function(data) {

            if (register_repeat_flag) return;
            register_repeat_flag = true;

            $.ajax({
                type: "POST",
                dataType: "JSON",
                url:  ns.url("shop/login/register"),
                data: data.field,
                success: function(res) {
                    if (res.code == 0) {
                        layer.msg(res.message,{anim: 5,time: 500},function () {
                            window.location = ns.url('shop/index/index');
                        });
                    } else {
                        layer.msg(res.message);

                        register_repeat_flag = false;
                        verificationCode();
                    }
                }
            })
        });
        
        /**
         * 表单验证
         */
        form.verify({
            repass: function(value) {
                if (value != $("#set_pass").val()) {
                    return "两次输入密码不一致";
                }
            },
            userName: function(value) {
                if (!value.trim()) {
                    return "账号不能为空";
                }
            },
            password: function(value) {
                if (!value.trim()) {
                    return "密码不能为空";
                }
            },
            verificationCode: function(value) {
                if (!value.trim()) {
                    return "验证码不能为空";
                }
            }

        });
    });

    $("body").on("blur",".login-content .login-input",function(){
        $(this).removeClass("login-input-select");
    });
    $("body").on("focus",".login-content .login-input",function(){
        $(this).addClass("login-input-select");
    });

    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
			$("#reg_btn").trigger("click");
        }
    });

    function login(){
        location.href = ns.url("shop/login/login");
    }
</script>
{/block}